package com.xbongbong.pro.rabbitmq.consumer;/*/*/

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.pojo.dto.MessageRabbitMqDTO;
import com.xbongbong.pro.message.pojo.vo.MessageRabbitMqVO;
import com.xbongbong.pro.rabbitmq.binders.SystemConsumerBinder;
import com.xbongbong.pro.rabbitmq.constant.SystemBinderConstant;
import com.xbongbong.pro.rabbitmq.feign.SystemFeignClient;
import feign.RetryableException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.handler.annotation.Header;

import javax.annotation.Resource;

/**
 * 系统 消息消费者
 * @author 魏荣杰
 * @date 2019/3/1 14:24
 * @since v1.0
 * @version v1.0
 */
@EnableBinding(SystemConsumerBinder.class)
public class SystemListener {
    private static Logger logger = LoggerFactory.getLogger(SystemListener.class);

    @Resource
    private SystemFeignClient systemFeignClient;

    @StreamListener(value = SystemBinderConstant.NEW_SIGNATURE_INPUT)
    public void newSignatureReceive(MessageRabbitMqDTO message, @Header("type")PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case NEW_SIGNATURE_PUSH:
                    response = systemFeignClient.newSignature(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("SystemListener.newSignatureReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("SystemListener.newSignatureReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = SystemBinderConstant.QUESTIONNAIRE_INPUT)
    public void questionnaireReceive(MessageRabbitMqDTO message, @Header("type")PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case QUESTIONNAIRE_PUSH:
                    response = systemFeignClient.questionnaire(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("SystemListener.questionnaireReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("SystemListener.questionnaireReceive Exception, type=" + type, e);
        }
    }


    @StreamListener(value = SystemBinderConstant.RENEW_INPUT)
    public void renewReceive(MessageRabbitMqDTO message, @Header("type")PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case RENEW_PUSH:
                    response = systemFeignClient.renew(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("SystemListener.renewReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("SystemListener.renewReceive Exception, type=" + type, e);
        }
    }


    @StreamListener(value = SystemBinderConstant.ACTIVITY_OPERATION_INPUT)
    public void activityOperationReceive(MessageRabbitMqDTO message, @Header("type")PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case ACTIVITY_OPERATION_PUSH:
                    response = systemFeignClient.activityOperation(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("SystemListener.activityOperationReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("SystemListener.activityOperationReceive Exception, type=" + type, e);
        }
    }


    @StreamListener(value = SystemBinderConstant.INVITE_STAFF_INPUT)
    public void inviteStaffReceive(MessageRabbitMqDTO message, @Header("type")PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case INVITE_STAFF_PUSH:
                    response = systemFeignClient.inviteStaff(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("SystemListener.inviteStaffReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("SystemListener.inviteStaffReceive Exception, type=" + type, e);
        }
    }
}
